#pragma once

#include "CInterlockedReferenceCount.h"

class AtlasNode :
	public CInterlockedReferenceCount
{

public: 
	static HRESULT Create(const struct XRECT_WH & bounds, AtlasNode * * ppNode);
	HRESULT Add(XUINT32 requestWidth, XUINT32 requestHeight, AtlasNode * * ppNode);
	void Remove(AtlasNode * pNodeToRemove, XBOOL * pRemoved);
	XBOOL IsEmpty();
	AtlasNode * GetRight();
	AtlasNode * GetBottom();
	struct XRECT_WH GetSubrect();

protected: 
	AtlasNode(const struct XRECT_WH &);
	virtual ~AtlasNode();
private: 
	void UpdateLargestAvailableDimensions();
	XINT32 GetLargestFreeWidth();
	XINT32 GetLargestFreeHeight();
	XINT32 GetLargestFreeArea();
	void StoreIfLarger(XINT32 width, XINT32 height, XINT32 area, XINT32 * pLargestWidth, XINT32 * pLargestHeight, XINT32 * pLargestArea);
	XBOOL m_isSplit;
	AtlasNode * m_pRightChild;
	AtlasNode * m_pBottomChild;
	AtlasNode * m_pInnerChild;
	struct XPOINT m_topLeftOrigin;


private: 
	struct XSIZE m_totalSize;
	struct XSIZE m_topLeftAvailableSize;
	struct XSIZE m_topLeftContentSize;
	XINT32 m_largestFreeWidth;
	XINT32 m_largestFreeHeight;
	XINT32 m_largestFreeArea;
};